import path from 'path'
import fs from 'fs/promises'
import Router from '@koa/router'
import { glob, globSync, globStream, globStreamSync, Glob } from 'glob'

export default app => {
  const router = new Router()

  router.get('/api/message', async ctx => {
    const returnData = []

    const files = await glob(`data/*`, { ignore: 'node_modules/**' })
    // 按文件名升序排序
    files.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))

    console.log('files', files)

    for (const file of files) {
      const content = await fs.readFile(file, 'utf-8')
      returnData.push(content)
    }

    ctx.body = {
      code: 0,
      data: returnData,
    }
  })

  app.use(router.routes()).use(router.allowedMethods())
}
